<?php

namespace App\LayAdmin\Controllers;

use App\Exceptions\ExceptionApi;
use App\LayAdmin\Components\Form;
use App\LayAdmin\Components\Table;
use App\LayAdmin\Components\TableFieldSearch;
use App\LayAdmin\Components\TableItemOperate;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\LayAdmin\Components\TableData;

class UserController extends BaseController
{
    use TraitCRUD;

    protected $modelClass=User::class;

    public function index(Request $request) :Response {
        $page = $request->input('page',null);
        $dataUrl = "/admin/users/data";
        if($page!==null){
            $dataUrl .= "?".$request->getQueryString();
        }
        $table = new Table($dataUrl);

        $this->tableFilter($table);
        $this->tableButtons($table);

        $this->tableCols($table);

        $this->tableItemOpt($table);

        return new Response($table->render());
    }

    /** 搜索过滤 */
    private function tableFilter(Table &$table){
        $time = (new TableFieldSearch('time_begin',''))->searchTimeInput(['placeholder'=>'选择开始时间']);
        $table->setSearchField($time);
        $time = (new TableFieldSearch('time_end',''))->searchTimeInput(['placeholder'=>'选择结束时间']);
        $table->setSearchField($time);
    }


    /**
     * 批量操作按钮组
     */
    private function tableButtons(Table $table){
//        $table->addBtnPostToUrl('/admin/videos/batch-delete', '批量删除',['layui-btn-danger']);
    }

    /**
     * 数据操作列
     */
    private function tableItemOpt(Table $table, $weight=false){
        $table->itemOperate(function (TableItemOperate $operate)use($weight){
//            $editUrl = "\"/admin/user/edit?id=\"+data.id";
//            $operate->setEditBtn($editUrl);
            $delUrl = "\"/admin/user/delete\"";
            $operate->setDeleteBtn($delUrl);
        });
    }

    /**
     * 数据表格列
     */
    private function tableCols(Table &$table){
        $table->checkbox();

        $table->field('id', 'ID')->setWidth(80)->setSort();
        $table->field('nickname', '用户昵称')->setWidth(150)->search();
        $table->field('mobile', '手机号')->setWidth(200)->search();
        $table->field('created_at', '创建日期')->setWidth(160)->setSort();
    }
    public function data(Request $request):Response{
        $page = $request->input('page',1);
        $limit = $request->input('limit',30);
        $query = $this->modelClass::query();

        $searchParams = $request->input('searchParams');
        if($searchParams){
            $filter = json_decode($searchParams, true);
            $this->modelClass::filterData($query, $filter);
        }

        $list = $query->orderByDesc('id')->paginate($limit, ["*"], 'page', $page);

        $items = $list->items();
        foreach ($items as &$item){
//            $item = VideoResponse::toArray($item);
//            $item['is_recommend'] = $item['is_recommend']==0 ? '否' : '是';
//            $item['image'] = "<img src=\"{$item['image']}\">";
//            $item['author_img'] = "<img src=\"{$item['author_img']}\">";
        }
        $total = $list->total();
        $response = (new TableData($items, $total))->toArray(); // ['code'=>0,'count'=>$total,'msg'=>'','data'=>$items];
        return new Response($response);
    }

    public function edit(Request $request):Response{
        $model = $this->getModel($request);
        $form = new Form("/admin/user/edit");
        $form->field('id', $model->id)->hide();
        $form->field('nickname', $model->nickname)->label('昵称')->textInput();
        $form->field('mobile', $model->mobile)->label('手机')->textInput();
        $form->submit();
        return new Response($form->render());
    }

    public function update(Request $request):Response{
        $model = $this->getModel($request);
        $data = $request->input('data');
        $model->nickname = $data['nickname'];
        $model->mobile = $data['mobile'];
        if($model->save()){
            return $this->successApi();
        }
        throw new ExceptionApi('更新失败');
    }
}
